package com.xiaokaceng.openci.web.controller.developer;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;

import org.dayatang.querychannel.Page;
import org.openkoala.koala.auth.ss3adapter.AuthUserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaokaceng.openci.application.DeveloperApplication;
import com.xiaokaceng.openci.domain.Developer;
import com.xiaokaceng.openci.dto.DeveloperDto;
import com.xiaokaceng.openci.web.controller.BaseController;
import com.xiaokaceng.openci.web.dto.ResultDto;
import com.xiaokaceng.openci.web.dto.ResultMsgDto;

@Controller
@RequestMapping("/developer")
public class DeveloperController extends BaseController {

	@Inject
	private DeveloperApplication developerApplication;
	
	@ResponseBody
    @RequestMapping("/pagingquery")
	public Page<DeveloperDto> pagingQuery(int page, int pagesize, Developer example) {
		return developerApplication.pagingQeuryDevelopers(example, page, pagesize);
	}
	
	@ResponseBody
    @RequestMapping("/create")
	public ResultDto createDeveloper(Developer developer) {
		if (developerApplication.checkDeveloperIdIsExist(developer.getDeveloperId())) {
			return ResultMsgDto.createFailure("开发者ID已存在!");
		}
		developerApplication.createDeveloper(developer);
		return ResultDto.createSuccess();
	}
	
	@ResponseBody
    @RequestMapping("/update")
	public ResultDto updateDeveloper(Developer developer) {
		developerApplication.updateDeveloper(developer);
		return ResultDto.createSuccess();
	}
	
	@ResponseBody
    @RequestMapping("/abolish")
	public ResultDto abolishDeveloper(Developer developer) {
		developerApplication.abolishDeveloper(developer);
		return ResultDto.createSuccess();
	}
	
	@ResponseBody
    @RequestMapping(value = "/abolish_developers", method = RequestMethod.POST, consumes = "application/json")
	public ResultDto abolishDevelopers(@RequestBody Developer[] developers) {
		developerApplication.abolishDevelopers(Arrays.asList(developers));
		return ResultDto.createSuccess();
	}
	
	@ResponseBody
	@RequestMapping("/findby/developerId/{developerId}")
	public List<DeveloperDto> findByDeveloperId(@PathVariable String developerId) {
		return developerApplication.findByDeveloperId(developerId);
	}
	
	@ResponseBody
	@RequestMapping("/isexist/{developerId}")
	public boolean checkDeveloperIdIsExist(@PathVariable String developerId) {
		return developerApplication.checkDeveloperIdIsExist(developerId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/get-name", produces = "application/json;charset=UTF-8")
	public String getName() {
		Developer developer = developerApplication.getByDeveloperId(AuthUserUtil.getLoginUser().getUsername());
		if (developer == null) {
			return "";
		}
		return developer.getName();
	}
	
}
